package com.sky.alogrithm.chapter02_sort;

import static com.sky.alogrithm.chapter02_sort.Example.less;

/**
 * 归并排序
 * 原理：将两个不同的有序数组归并到第三个数组中
 */
public class Merge {

    public static void merge(Comparable[] a, int lo, int mid, int hi){
        int i = lo, j = mid + 1;
        Comparable[] aux = new Comparable[a.length];

        for(int k = lo; k <= hi; k++){
            aux[k] = a[k];
        }

        for(int k = lo; k <= hi; k++){
            if(i > mid){
                a[k] = aux[j++];
            }else if (j > hi){
                a[k] = aux[i++];
            }else if(less(aux[j], aux[i])){
                a[k] = aux[j++];
            }else{
                a[k] = aux[i++];
            }
        }
    }
}
